package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.MessageNotificationDTO;
import com.pshcp.domain.model.MessageNotification;
import com.pshcp.mapper.MessageNotificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统消息表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class MessageNotificationService {

    @Autowired
    private MessageNotificationMapper messageNotificationMapper;

    public int getUnReadMessageNotificationCount(Integer userType, Long userId) {
        Example example = new Example(MessageNotification.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("userType", userType);
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("readFlag", 0);
        return messageNotificationMapper.selectCountByExample(example);
    }

    public void readAllMessageNotification(Integer userType, Long userId) {
        messageNotificationMapper.readAllMessageNotification(userType, userId);
    }

    public void delAllMessageNotification(Integer userType, Long userId) {
        messageNotificationMapper.delAllMessageNotification(userType, userId);
    }

    public PageList<MessageNotification> list(MessageNotificationDTO messageNotificationDTO) {
        if (messageNotificationDTO.getPageNum() != null && messageNotificationDTO.getPageSize() != null) {
            PageHelper.startPage(messageNotificationDTO.getPageNum(), messageNotificationDTO.getPageSize());
        }
        Example example = new Example(MessageNotification.class);
        Example.Criteria criteria = example.createCriteria();
        if (messageNotificationDTO.getStatus() != null) {
            criteria.andEqualTo("status", messageNotificationDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if (messageNotificationDTO.getUserType() != null) {
            criteria.andEqualTo("userType", messageNotificationDTO.getUserType());
        }
        if (messageNotificationDTO.getUserId() != null) {
            criteria.andEqualTo("userId", messageNotificationDTO.getUserId());
        }
        example.setOrderByClause(" created_time desc");
        List<MessageNotification> list = messageNotificationMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<MessageNotification> select(MessageNotification messageNotification) {
        return messageNotificationMapper.select(messageNotification);
    }

    public int insert(MessageNotification messageNotification) {
        messageNotification.setId(SystemUtil.getId());
        messageNotification.setStatus(CommonStatusEnum.INIT.getCode());
        messageNotification.setCreatedTime(new Date());
        return messageNotificationMapper.insert(messageNotification);
    }

    public int update(MessageNotification messageNotification) {
        messageNotification.setUpdateTime(new Date());
        return messageNotificationMapper.updateByPrimaryKeySelective(messageNotification);
    }

    public MessageNotification findById(Long id) {
        return messageNotificationMapper.selectByPrimaryKey(id);
    }

    public List<MessageNotification> findByIds(List<Long> ids) {
        Example example = new Example(MessageNotification.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return messageNotificationMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(MessageNotification.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        MessageNotification messageNotification = new MessageNotification();
        messageNotification.setStatus(CommonStatusEnum.DEL.getCode());
        return messageNotificationMapper.updateByExampleSelective(messageNotification, example);
    }

}
